<?php

function createPagination() {
    $pageination = "";
    $page_name = $_SERVER['PHP_SELF'] . "?page=views";

    if (isset($_GET['start'])) {
        $start = $_GET['start'];
    } else {
        $start = null;
    }

    $startfrom = ($start - 0);
    $limit = 5;
    // # of records to be shown per
    $correntPage = $startfrom + $limit;
    $back = $startfrom - $limit;
    $next = $startfrom + $limit;

    $querySelect = " SELECT * FROM tbl_students ";
    $getResult = mysql_query($querySelect);
    $nume = mysql_num_rows($getResult);

    $query = " SELECT * FROM tbl_students order by stu_date_create desc limit $startfrom, $limit  ";
    $result = mysql_query($query);
    //table view

    $i = 1;

    while ($row = mysql_fetch_array($result)) {
            
        if($i % 2) $setclass ="old";
        else $setclass ="eve";
        
        $pageination .= '<tr class="'.$setclass.'" >';
        $pageination .= '
        <td id="firstName">'.$row["stu_first_name"].'</td>
        <td id="lastName">'.$row["stu_last_name"].'</td>
        <td width="120" align="center">';
        
        if ($row["stu_sex"] == "M") $pageination.="Male";
        else $pageination.="Female";
        
        $pageination.='</td>
        <td>' . $row["stu_telephone"] . '</td>
        <td>' . $row["stu_email"] . '</td>
        <td align="center"><img src="uploads/thumbs/thumb_'.$row['stu_photo'].'" title="" alt="" /></td>
        <td align="center"><pre> <a href="index.php?page=form&action=edit&id='.$row['stu_id'].'">Edit</a> | <a href="controls/action.php?action=delete&id='.$row['stu_id'].'" onClick="javascript: return confirmDelete()">Delete</a> </pre></td>';
        $pageination .= '</tr>';
        $i++;
    }
    $pageination .= '</table></div></div><br /><div>';

    //End table view

    //pageination============//
    
    if ($nume > $limit) {
        $pageination .= "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";

        if ($back >= 0) {
            $pageination .= "<a href='$page_name&start=$back'><font face='Verdana' size='2'>PREV</font></a>";
        }

        $pageination .= "</td><td align=center width='30%'>";
        $i = 0;
        $l = 1;
        for ($i = 0; $i < $nume; $i = $i + $limit) {
            if ($i <> $startfrom) {
                $pageination .= " <a href='$page_name&start=$i'><font face='Verdana' size='2'>$l</font></a> ";
            } else { $pageination .= "<font face='Verdana' size='4' color=red>$l</font>";
            }
            $l++;
        }

        $pageination .= "</td><td  align='right' width='30%'>";

        if ($correntPage < $nume) {
            $pageination .= "<a href='$page_name&start=$next'><font face='Verdana' size='2'>NEXT</font></a>";
        }
        $pageination .= "</td></tr></table>";

    }
    return $pageination;
}
?>
<html>
<head>

<link href="styles/style.css" type="text/css" rel="stylesheet" />

<script type="text/javascript" language="JavaScript1.8">
    function confirmDelete() {
        return confirm("Are you sure you wish to delete this entry?");
    }

    function changeImage(itemChang) {
        //alert(itemChang.src);
        //itemChang.src = "images/icons/user-add-hover.png";
    }

</script>
</head>

<body>

<div id="wraper">
<div id="sub_title"> Student Management
<hr id="line" />
</div>
<div id="action_top">
<!--	<img name="btn" src="images/icons/user-add.png"
onmouseover="btn.src='images/icons/user-add-hover.png';"
onmouseout="btn.src='images/icons/user-add.png';" />	-->

<a href="index.php?page=form&action=add" > <img name="image" src="images/icons/user-add.png"
onmouseover="javascript: image.src='images/icons/user-add-hover.png';"
onmouseout="javascript: image.src='images/icons/user-add.png';" /> <br />
Add new</a> </div>
<div>
<table class="list" cellpadding="0" cellspacing="0" border="1">
<tr id="table_top">
<th >First Name</th>
<th >Last Name</th>
<th >Sex</th>
<th >Telephone</th>
<th>Email</th>
<th >Photo</th>
<th >Actions</th>
</tr>

<!--  <tr class="old">
<td colspan="6"> Test</td>
</tr>-->

<?php
/*
 * $i = 1;
 //$result = mysql_query("SELECT * FROM students");

 while ($row = mysql_fetch_array($result)){
 ?>
 <tr <?php echo(($i % 2) ? 'class=old' : 'class=eve'); ?>>
 <td id="firstName"><?php echo $row['stu_first_name']; ?><
 /td>
 <td id="lastName"><?php echo $row['stu_last_name']; ?><
 /td>
 <td width="120" align="center"><?php  echo( $row['stu_sex']== 'M'?"Male":"Female" )?><
 /td>
 <td><?php echo $row['stu_telephone']; ?><
 /td>
 <td><?php echo $row['stu_email']; ?><
 /td>
 <td align="center"><img src="uploads/thumbs/thumb_<?php echo $row['stu_photo']; ?>" title="" alt="" /></td>
 <td align="center"><pre> <a href="index.php?page=form&action=edit&id=<?php echo $row['stu_id']?>" >Edit</a> | <a href="controls/action.php?action=delete&id=<?php echo $row['stu_id']?>" onClick="javascript: return confirmDelete()">Delete</a> </pre></td>
 </tr>
 <?php
 $i++;
 }
 * */
?>
<!--
</table>
</div>
</div>
<br />
<div> -->
  
<?php
echo createPagination();
/*
 if ($nume > $limit) {
 echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";

 if ($back >= 0) {
 print "<a href='$page_name&start=$back'><font face='Verdana' size='2'>PREV</font></a>";
 }

 echo "</td><td align=center width='30%'>";
 $i = 0;
 $l = 1;
 for ($i = 0; $i < $nume; $i = $i + $limit) {
 if ($i <> $startfrom) {
 echo " <a href='$page_name&start=$i'><font face='Verdana' size='2'>$l</font></a> ";
 } else { echo "<font face='Verdana' size='4' color=red>$l</font>";
 }
 $l++;
 }

 echo "</td><td  align='right' width='30%'>";

 if ($correntPage < $nume) {
 print "<a href='$page_name&start=$next'><font face='Verdana' size='2'>NEXT</font></a>";
 }
 echo "</td></tr></table>";

 }

 */
?>
</div>
</body></html>